package codetop.microsoft.T5;

/**
 * @Author: 18362
 * @Create: 2022-08-13 23:21:55 星期六
 */
class Solution {
    public String longestPalindrome(String s) {
        int start = 0, maxLen = 0;
        for (int i=0; i<s.length(); ++i) {
            int j = 1;
            while (i+j < s.length() && i-j >= 0) {
                if (s.charAt(i+j) != s.charAt(i-j)) {
                    break;
                }
                j++;
            }
            j--;
            if (j * 2 + 1 > maxLen) {
                maxLen = j * 2 + 1;
                start = i - j;
            }
        }
        for (int i=0; i<s.length()-1; ++i) {
            int j = 0;
            while (i-j >= 0 && i+1+j < s.length()) {
                if (s.charAt(i-j) != s.charAt(i+1+j)) {
                    break;
                }
                j++;
            }
            j--;
            if (j * 2 + 2 > maxLen) {
                maxLen = j * 2 + 2;
                start = i - j;
            }
        }
        return s.substring(start, start+maxLen);
    }
}